<template>
  <nav class="app-navbar">
    <div class="navbar-container">
      <div class="navbar-content">
        <!-- 左侧：Logo -->
        <div class="navbar-left">
          <router-link to="/" class="logo-link">
            <div class="logo">
              <i class="fas fa-gamepad"></i>
              <span class="logo-text">游戏平台</span>
            </div>
          </router-link>
        </div>

        <!-- 右侧：用户信息和设置 -->
        <div class="navbar-right">
          <div class="user-section">
            <!-- 用户头像和信息 -->
            <div class="user-info" @click="navigateToProfile">
              <div class="user-avatar">
                <img 
                  :src="userStore.userInfo.avatar || '/default-avatar.svg'" 
                  :alt="userStore.userInfo.username || '用户'"
                  class="avatar-img"
                >
                <div v-if="userStore.isAuthenticated" class="online-indicator"></div>
              </div>
              <div class="user-details">
                <div class="username">{{ userStore.userInfo.username || '玩家123' }}</div>
                <div class="user-tier">
                  <div class="tier-badge" :class="getTierClass(userStore.userInfo.totalScore || 0)">
                    {{ getTierName(userStore.userInfo.totalScore || 0) }}
                  </div>
                </div>
              </div>
              <div class="profile-label">个人中心</div>
            </div>

            <!-- 设置按钮 -->
            <button @click="navigateToSettings" class="settings-btn" title="设置">
              <i class="fas fa-cog"></i>
            </button>

            <!-- 下拉菜单 -->
            <div class="dropdown" v-if="showDropdown">
              <div class="dropdown-menu">
                <router-link to="/profile" class="dropdown-item">
                  <i class="fas fa-user"></i>
                  <span>个人资料</span>
                </router-link>
                <router-link to="/settings" class="dropdown-item">
                  <i class="fas fa-cog"></i>
                  <span>设置</span>
                </router-link>
                <router-link to="/ranking" class="dropdown-item">
                  <i class="fas fa-trophy"></i>
                  <span>排行榜</span>
                </router-link>
                <div class="dropdown-divider"></div>
                <button @click="logout" class="dropdown-item logout-btn">
                  <i class="fas fa-sign-out-alt"></i>
                  <span>退出登录</span>
                </button>
              </div>
            </div>
          </div>

          <!-- 通知按钮 -->
          <button class="notification-btn" title="消息通知">
            <i class="fas fa-bell"></i>
            <span v-if="notificationCount > 0" class="notification-badge">{{ notificationCount }}</span>
          </button>
        </div>
      </div>
    </div>
  </nav>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user.js'

const router = useRouter()
const userStore = useUserStore()

// 段位判断函数
const getTierClass = (score) => {
  if (score >= 401) return 'challenger';
  if (score >= 351) return 'grandmaster';
  if (score >= 301) return 'master';
  if (score >= 251) return 'diamond';
  if (score >= 201) return 'platinum';
  if (score >= 151) return 'gold';
  if (score >= 101) return 'silver';
  if (score >= 51) return 'bronze';
  return 'iron';
};

const getTierName = (score) => {
  if (score >= 401) return '赋能';
  if (score >= 351) return '神话';
  if (score >= 301) return '超凡';
  if (score >= 251) return '钻石';
  if (score >= 201) return '铂金';
  if (score >= 151) return '黄金';
  if (score >= 101) return '白银';
  if (score >= 51) return '青铜';
  return '黑铁';
};

// 响应式数据
const showDropdown = ref(false)
const notificationCount = ref(3) // 模拟通知数量

// 计算属性
const isLoggedIn = computed(() => userStore.isAuthenticated)

// 方法
const navigateToProfile = () => {
  router.push('/profile')
  showDropdown.value = false
}

const navigateToSettings = () => {
  router.push('/settings')
  showDropdown.value = false
}

const toggleDropdown = () => {
  showDropdown.value = !showDropdown.value
}

const logout = async () => {
  try {
    await userStore.logout()
    router.push('/auth/login')
    if (window.$notification) {
      window.$notification.success('退出登录成功')
    }
  } catch (error) {
    console.error('退出登录失败:', error)
    if (window.$notification) {
      window.$notification.error('退出登录失败')
    }
  }
  showDropdown.value = false
}

// 点击外部关闭下拉菜单
const handleClickOutside = (event) => {
  const dropdown = event.target.closest('.dropdown, .user-info')
  if (!dropdown) {
    showDropdown.value = false
  }
}

// 生命周期
onMounted(() => {
  document.addEventListener('click', handleClickOutside)
})

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside)
})
</script>

<style scoped>
.app-navbar {
  background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
  transition: all 0.3s ease;
}

.navbar-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.navbar-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 70px;
}

.navbar-left {
  display: flex;
  align-items: center;
}

.logo-link {
  text-decoration: none;
  color: inherit;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
  color: white;
  font-size: 1.5rem;
  font-weight: bold;
  transition: transform 0.3s ease;
}

.logo:hover {
  transform: scale(1.05);
}

.logo i {
  font-size: 2rem;
  color: #3498db;
}

.logo-text {
  font-family: 'Microsoft YaHei', sans-serif;
}

.navbar-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-section {
  display: flex;
  align-items: center;
  gap: 15px;
  position: relative;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 25px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.user-info:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
}

.user-avatar {
  position: relative;
  width: 40px;
  height: 40px;
}

.avatar-img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid rgba(255, 255, 255, 0.3);
  transition: border-color 0.3s ease;
}

.user-info:hover .avatar-img {
  border-color: rgba(255, 255, 255, 0.6);
}

.online-indicator {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 12px;
  height: 12px;
  background: #2ecc71;
  border-radius: 50%;
  border: 2px solid white;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

.user-details {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.username {
  color: white;
  font-weight: 600;
  font-size: 0.95rem;
  margin-bottom: 2px;
}

.user-tier {
  transform: scale(0.8);
  transform-origin: left;
}

.tier-badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 15px;
  font-size: 0.85rem;
  font-weight: bold;
  color: white;
  text-align: center;
}

.tier-badge.iron { background-color: #4b5563; }
.tier-badge.bronze { background-color: #a16207; }
.tier-badge.silver { background-color: #9ca3af; }
.tier-badge.gold { background-color: #fbbf24; color: #92400e; }
.tier-badge.platinum { background-color: #e5e7eb; color: #374151; }
.tier-badge.diamond { background-color: #4f46e5; }
.tier-badge.master { background-color: #059669; }
.tier-badge.grandmaster { background-color: #dc2626; }
.tier-badge.challenger { background-color: #7c3aed; }

.profile-label {
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.85rem;
  font-weight: 500;
  margin-left: 8px;
}

.settings-btn {
  width: 45px;
  height: 45px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  transition: all 0.3s ease;
}

.settings-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: rotate(90deg) translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.notification-btn {
  position: relative;
  width: 45px;
  height: 45px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  transition: all 0.3s ease;
}

.notification-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.notification-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background: #e74c3c;
  color: white;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: bold;
  animation: bounce 1s infinite;
}

@keyframes bounce {
  0%, 20%, 50%, 80%, 100% { transform: translateY(0); }
  40% { transform: translateY(-5px); }
  60% { transform: translateY(-3px); }
}

.dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 10px;
  z-index: 1001;
}

.dropdown-menu {
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  padding: 8px 0;
  min-width: 200px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  animation: slideDown 0.3s ease;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  color: #333;
  text-decoration: none;
  font-size: 0.95rem;
  transition: all 0.3s ease;
  border: none;
  background: none;
  width: 100%;
  cursor: pointer;
}

.dropdown-item:hover {
  background: #f8f9fa;
  color: #2c3e50;
}

.dropdown-item i {
  width: 16px;
  text-align: center;
}

.dropdown-divider {
  height: 1px;
  background: #e9ecef;
  margin: 8px 0;
}

.logout-btn {
  color: #e74c3c;
}

.logout-btn:hover {
  background: #fee;
  color: #c0392b;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .navbar-container {
    padding: 0 15px;
  }
  
  .navbar-content {
    height: 60px;
  }
  
  .logo {
    font-size: 1.3rem;
  }
  
  .logo i {
    font-size: 1.5rem;
  }
  
  .user-info {
    padding: 6px 12px;
  }
  
  .profile-label {
    display: none;
  }
  
  .user-details {
    display: none;
  }
  
  .user-avatar {
    width: 35px;
    height: 35px;
  }
  
  .settings-btn,
  .notification-btn {
    width: 40px;
    height: 40px;
    font-size: 1rem;
  }
  
  .dropdown-menu {
    right: -20px;
    min-width: 180px;
  }
}

@media (max-width: 480px) {
  .logo-text {
    display: none;
  }
  
  .navbar-right {
    gap: 10px;
  }
  
  .user-info {
    padding: 4px 8px;
  }
}
</style>
